<?php defined('SYSPATH') or die('No direct script access.');

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2020/10/09 10:08
 * Desc: 游记模块
 */
class Model_Api_V2_Notes
{
    /**
     * 类型id
     */
    const TYPE_ID = 101;

    /**
     * @function 游记搜索
     * @param array $params
     * @param int $mid
     * @return mixed
     */
    public static function search($params, $mid = 0)
    {

        /**
         * @var $page
         * @var $pagesize
         * @var $keyword
         * @var $dest_id
         * @var $sort_type
         */
        extract($params);

        $dest_id = empty($dest_id) ? 0 : $dest_id;

        $builder = DB::select('a.*', DB::expr('sline_a.shownum+sline_a.read_num as nums'))
            ->from(array('notes', 'a'));

        if (!empty($dest_id)) {
            $builder->join(array('kindorderlist', 'b'), 'left');
        } else {
            $builder->join(array('allorderlist', 'b'), 'left');
        }
        $builder->on('a.id', '=', DB::expr('sline_b.aid'));
        $builder->on('b.typeid', '=', DB::expr(self::TYPE_ID));

        $builder->where('a.ishidden', '=', 0);
        if (!empty($mid)) {
            $builder->where('a.memberid', '=', $mid);
        }
        if (!empty($dest_id)) {
            $builder->where(DB::expr(" AND FIND_IN_SET($dest_id,sline_a.kindlist) "));
        }
        if ($status !== false) {
            $builder->where('a.status', '=', $status);
        }
        if (!empty($keyword)) {
            $builder->where('a.title', 'like', "%$keyword%");
        }
        if (!empty($sort_type)) {
            if ($sort_type == 1) {
                $builder->order_by(DB::expr('sline_a.modtime DESC,sline_a.addtime DESC,IFNULL(sline_b.displayorder,9999) ASC'));
            }
        } else {
            $builder->order_by(DB::expr('IFNULL(sline_b.displayorder,9999) asc,nums DESC,sline_a.modtime desc,sline_a.addtime DESC'));
        }

        $offset = St_Page::offset($page, $pagesize);
        $builder->offset($offset)->limit($pagesize);

        return $builder->execute()->as_array();
    }

    /**
     * 游记积分策略处理
     * @param $mid
     * @return void
     * @throws Kohana_Exception
     */
    public static function notes_jifen($mid)
    {
        //积分策略
        $jifen = DB::select('value', 'frequency_type', 'frequency')
            ->from('jifen')
            ->where('typeid', '=', self::TYPE_ID)
            ->where('label', '=', 'sys_write_notes')
            ->where('isopen', '=', 1)
            ->execute()
            ->current();
        if ($jifen) {
            switch ($jifen['frequency_type']) {
                case 0://每次
                    Model_Api_V2_Notes::send_jifen($mid, $jifen['value']);
                    break;
                case 1://仅一次
                    $count = DB::select()->from('jifen_record')
                        ->where('memberid', '=', $mid)
                        ->where('label', '=', 'sys_write_notes')
                        ->execute()
                        ->count();
                    if (!$count) {
                        Model_Api_V2_Notes::send_jifen($mid, $jifen['value']);
                    }
                    break;
                case 2://每天前N次
                    $count = DB::select()
                        ->from('jifen_record')
                        ->where('memberid', '=', $mid)
                        ->where('label', '=', 'sys_write_notes')
                        ->where('addtime', '>', strtotime(date('Y-m-d')))
                        ->execute()
                        ->count();
                    if ($count < $jifen['frequency']) {
                        Model_Api_V2_Notes::send_jifen($mid, $jifen['value']);
                    }
                    break;
                case 3://总共N次
                    $count = DB::select()
                        ->from('jifen_record')
                        ->where('memberid', '=', $mid)
                        ->where('label', '=', 'sys_write_notes')
                        ->execute()
                        ->count();
                    if ($count < $jifen['frequency']) {
                        Model_Api_V2_Notes::send_jifen($mid, $jifen['value']);
                    }
                    break;
            }
        }
    }

    /**
     * 赠送积分
     * @param $mid
     * @param $jifen
     * @throws Kohana_Exception
     */
    public static function send_jifen($mid, $jifen)
    {
        $sql  = "UPDATE `sline_member` SET jifen=jifen+{$jifen} WHERE mid=" . $mid;
        $flag = DB::query(Database::UPDATE, $sql)->execute();
        if ($flag) {
            //会员积分使用记录
            $insert_arr = array(
                'memberid' => $mid,
                'content' => "发布游记获得{$jifen}积分",
                'jifen' => $jifen,
                'type' => 2,
                'addtime' => time()
            );
            DB::insert('member_jifen_log', array_keys($insert_arr))->values(array_values($insert_arr))->execute();
            //积分操作记录(用于积分次数限制)
            $insert = array(
                'memberid' => $mid,
                'label' => 'sys_write_notes',
                'jifen' => $jifen,
                'addtime' => time()
            );
            DB::insert('jifen_record', array_keys($insert))->values(array_values($insert))->execute();
        }
    }
}